package com.goat.model.element;

import com.goat.model.manager.GameLoader;

import javax.swing.*;
import java.awt.*;
import java.util.Map;

public class MapObj extends BaseElement{
    private static Map<String,ImageIcon> imageMap= GameLoader.getImageIconMap();

    @Override
    public void showElement(Graphics g) {
        g.drawImage(getImageIcon().getImage(), getX(),getY(),getW(),getH(),null);
    }

    @Override
    public BaseElement createElement(String str) {
        //e.g:"BRICK,80,100"   str的格式
        String[] split = str.split(",");
        //注释掉下面代码后出现问题，空指针异常
        //解决问题：主线程处先载入图片才能载入其它元素，因为元素需要图片来初始化
        /*switch (split[0]){
            case "GRASS":setImageIcon(new ImageIcon("image/wall/grass.png"));break;
            case "BRICK":setImageIcon(new ImageIcon("image/wall/brick.png"));break;
            case "RIVER":setImageIcon(new ImageIcon("image/wall/river.png"));break;
            case "IRON":setImageIcon(new ImageIcon("image/wall/iron.png"));break;
        }*/

        setImageIcon(imageMap.get(split[0]));
        setX(Integer.parseInt(split[1]));
        setY(Integer.parseInt(split[2]));
        setW(getImageIcon().getIconWidth());
        setH(getImageIcon().getIconHeight());
        return this;
    }
}
